﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NeoModulusBusinessRulesDLLBuilder.Classes;
using NeoModulusBusinessRulesDLLBuilder.Dialogs;

namespace NeoModulusBusinessRulesDLLBuilder.UserControls
{
    public partial class usrObjectExplorer : UserControl
    {
        private NewRelationshipWizard _newRelationshipWizard;
        private NewVariableWizard _newVariableWizard;
        private NewFunctionWizard _newFuctionWizard;

        private ObjectDefinition _currentObject { get; set; }

        private usrObjectTree _currentObjectTree;
        public usrObjectTree CurrentObjectTree
        {
            set
            {
                _currentObjectTree = value;
                _currentObjectTree.OnObjectSelected += new usrObjectTree.ObjectSelected(_currentObjectTree_OnObjectSelected);

                
            }
        }

        void _currentObjectTree_OnObjectSelected(ObjectDefinition inObject)
        {
            _currentObject = inObject;
            LoadObject();
        }

        

        public usrObjectExplorer()
        {
            InitializeComponent();
        }

        private void txtObjectName_TextChanged(object sender, EventArgs e)
        {

        }

        private void btnSave_Click(object sender, EventArgs e)
        {

        }

        public void LoadObject()
        {
            if (_currentObject != null)
            {
                txtObjectName.Text = _currentObject.ObjectName;

                foreach( ObjectRelationship or in _currentObject.Relationships.Values)
                {
                    lvRelationships.Items.Add(new ListViewItem(new string[] { or.ObjectDef.ObjectName, or.RelationshipType.ToString(), "Click to Edit", "Click to Delete" }));
                }
            }
        }

        private void tbObjectExplorer_ButtonClick(object sender, ToolBarButtonClickEventArgs e)
        {
            if (e.Button == tbbAddRelationship)
            {
                _newRelationshipWizard = new NewRelationshipWizard();
                _newRelationshipWizard.CurrentObjects = _currentObjectTree.CurrentObjects;
                _newRelationshipWizard.Closed += new EventHandler(_newRelationshipWizard_Closed);
                _newRelationshipWizard.ShowDialog();
            } 
            else if(e.Button == tbbAddVariable)
            {
                _newVariableWizard = new NewVariableWizard();
                _newVariableWizard.ShowDialog();
            }
            else if (e.Button == tbbAddFunction)
            {
                _newFuctionWizard = new NewFunctionWizard();
                _newFuctionWizard.ShowDialog();
            }
        }

        void _newRelationshipWizard_Closed(object sender, EventArgs e)
        {
            if (sender is NewRelationshipWizard && _newRelationshipWizard.NewRelationship != null)
            {
                _currentObject.Relationships.Add(_newRelationshipWizard.NewRelationship.ObjectDef.ObjectName, _newRelationshipWizard.NewRelationship);
                lvRelationships.Items.Add(new ListViewItem(new string[] {_newRelationshipWizard.NewRelationship.ObjectDef.ObjectName, _newRelationshipWizard.NewRelationship.RelationshipType.ToString(), "Click to Edit", "Click to Delete"}));
                _currentObjectTree.SaveProject();
            }
        }
    }
}
